package domain;

import java.util.Scanner;

/**
 * @author cpp
 * @version 1.0
 * @date 2025/3/19 15:18
 * @description 鲜花业务层
 */
public class Flower01 {
    private Flower[][] flowers; // 二维数组存储订单
    private int row; // 当前行数
    private int column; // 每行的列数

    // 有参构造
    public Flower01(int row, int column) {
        this.row = row;
        this.column = column;
        flowers = new Flower[row][column];
    }

    public Flower01() {
    }

    // 初始化订单数据
    public void inital() {
        flowers[0][0] = new Flower(1001, "金色海岸-香槟玫瑰", 10, 318.0, "2016-12-12", "大蛋", "一般");
        flowers[1][0] = new Flower(1002, "初心永不变-白美规", 99, 538.0, "2016-12-20", "乐孩", "夹一束白玫瑰");
        flowers[2][0] = new Flower(1003, "梦里花开-紫色桔梗", 40, 148.0, "2016-12-24", "魏狗", "");
        flowers[3][0] = new Flower(1004, "心花怒发-粉色百合", 120, 188.0, "2016-12-26", "叶问", "");
        flowers[4][0] = new Flower(1005, "致青春-无声的爱", 1000, 219.0, "2016-12-18", "谁了", "");
        row = 5; // 初始化5行数据
    }

    // 查询所有订单
    public void queryAll() {
        System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
        for (int i = 0; i < row; i++) {
            if (flowers[i][0] != null) {
                System.out.println(flowers[i][0].show());
            }
        }
    }


    // 修改订单
    public void revise(int id) {
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < row; i++) {
            if (flowers[i][0] != null && flowers[i][0].getCodenumber() == id) {
                System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
                System.out.println(flowers[i][0].show());

                System.out.println("请输入修改后的鲜花名称:");
                String name = input.next();
                System.out.println("请输入修改后的销售数量:");
                int quantity = input.nextInt();
                System.out.println("请输入修改后的价格:");
                double price = input.nextDouble();
                System.out.println("请输入修改后的销售日期：(yyyy-MM-dd)");
                String date = input.next();
                System.out.println("请输入修改后的销售员:");
                String saler = input.next();
                System.out.println("请输入修改后的备注：");
                String remarks = input.next();

                System.out.println("你确定修改吗？(y/n)");
                String ok = input.next();
                if ("y".equalsIgnoreCase(ok)) {
                    flowers[i][0].setFlowername(name);
                    flowers[i][0].setSales(quantity);
                    flowers[i][0].setPrice(price);
                    flowers[i][0].setDate(date);
                    flowers[i][0].setSaler(saler);
                    flowers[i][0].setRemarks(remarks);
                    System.out.println("修改成功！");
                    return;
                } else {
                    System.out.println("修改已取消！");
                    return;
                }
            }
        }
        System.out.println("对不起，您要修改的订单不存在！");
    }

    // 删除订单
    public void deleteSale(int id) {
        for (int i = 0; i < row; i++) {
            if (flowers[i][0] != null && flowers[i][0].getCodenumber() == id) {
                System.out.println("编号\t鲜花名称\t销售数量\t价格\t销售日期\t销售员\t备注");
                System.out.println(flowers[i][0].show());

                System.out.println("你确定删除吗？(y/n)");
                String ok = new Scanner(System.in).next();
                if ("y".equalsIgnoreCase(ok)) {
                    // 将后面的订单前移
                    for (int j = i; j < row - 1; j++) {
                        flowers[j][0] = flowers[j + 1][0];
                    }
                    flowers[row - 1][0] = null; // 最后一位置空
                    row--; // 行数减1
                    System.out.println("删除成功！");
                    return;
                } else {
                    System.out.println("删除已取消！");
                    return;
                }
            }
        }
        System.out.println("对不起，您要删除的订单不存在！");
    }
}